package commons;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.util.Arrays;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
import javax.swing.UIManager;

@SuppressWarnings("serial")
public class GUI extends JFrame {

	JPanel[] row = new JPanel[5];
	JButton[] button = new JButton[22];
	static JRadioButton radButton=new JRadioButton("Rad");
	static JRadioButton degButton=new JRadioButton("Deg",true);
	String[] buttonStrings = { 	"7", "8", "9", "sin", 
								"4", "5", "6","x \u036F","\u0065 \u036F","cos",
								"1", "2","3","10 \u036F","tan", "\u03C0",
								"0", "\u00B7","C","log","\u00B1", "\u212f" };
	//212f
	int[] functionKey = { 3, 7, 8, 9, 13, 14, 15, 19, 20, 21};

	Dimension textDim = new Dimension(375, 35);
	Dimension regularDim = new Dimension(50, 40);
	Dimension additionalButton = new Dimension(75, 40);
	Font font = new Font("Universalia", Font.BOLD, 15);

	static JTextArea display = new JTextArea();

	/**
	 * Launches the calculator window
	 */
	
	
	public GUI(Runnable c) {

		super("Eternity-Calculator");
		setDesign();
		setSize(425, 300);
		setResizable(false);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		Numbers n=new Numbers();
		
		
		GridLayout grid = new GridLayout(5, 6);
		setLayout(grid);


		FlowLayout f1 = new FlowLayout(FlowLayout.CENTER);
		FlowLayout f2 = new FlowLayout(FlowLayout.CENTER, 1, 1);

		for (int i = 0; i < 5; i++)
			row[i] = new JPanel();

		row[0].setLayout(f1);

		for (int i = 1; i < 5; i++)
			row[i].setLayout(f2);

		degButton.setFont(font);
		degButton.setContentAreaFilled(false);
		degButton.setFocusable(false);
		degButton.addActionListener(c);
		radButton.setFont(font);
		radButton.setContentAreaFilled(false);
		radButton.setFocusable(false);
		radButton.addActionListener(c);
		
		for (int i = 0; i < 22; i++) {
			
			button[i] = new JButton();
			button[i].setText(buttonStrings[i]);
			button[i].setFont(font);
			int[] numbers={0,1,2,4,5,6,10,11,12,15,16,17,18,20,21};
			if(Arrays.binarySearch(numbers, i)>=0)
			{
				button[i].addActionListener(n); //For number keys and . and +/- and C keys actions are added of type Number class
			}else
			{
				button[i].addActionListener(c); //For all 7 function keys actions are added of type Calculator class
			}
			button[i].setFocusable(false);
			
		}

		display.setFont(font);
		display.setEditable(false);
		display.setPreferredSize(textDim);

		for (int i = 0; i < 22; i++) {
			
			/** searching array for specific button to set different size e.g. sin,cos etc. **/
			if (Arrays.binarySearch(functionKey, i) >= 0)
			{
				button[i].setPreferredSize(additionalButton);
			} else {
				button[i].setPreferredSize(regularDim);
			}
		}
		
		radButton.setPreferredSize(additionalButton);
		degButton.setPreferredSize(additionalButton);
		
		ButtonGroup group=new ButtonGroup();
		group.add(degButton);
		group.add(radButton);
		
		row[0].add(display);
		add(row[0]);

		for (int i = 0; i < 4; i++)
			row[1].add(button[i]);
		row[1].add(degButton);
		row[1].add(radButton);
		add(row[1]);

		for (int i = 4; i < 10; i++)
			row[2].add(button[i]);
		
		add(row[2]);

		for (int i = 10; i < 16; i++)
			row[3].add(button[i]);
		add(row[3]);

		for (int i = 16; i < 22; i++)
			row[4].add(button[i]);
		add(row[4]);

		setVisible(true);
		
	}
	
	public GUI() {
		
	}
	
	String str="";
	public void displayText(String s){
		str+=s;
		display.setText(str);
		
	}
	

	public final void setDesign() {
		try {
			UIManager
					.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
		} catch (Exception e) {
		}
	}
}
